public class test2 {
    //力扣  24. 两两交换链表中的结点
    public ListNode swapPairs(ListNode head) {
        if(head==null || head.next == null) return head;

        ListNode newhead = new ListNode(0);
        newhead.next = head;
        ListNode prev = newhead;
        ListNode cur = prev.next;
        ListNode next = cur.next;
        ListNode nnext = next.next;

        while(cur!=null && next!=null){
            //交换结点
            prev.next = next;
            next.next = cur;
            cur.next = nnext;
            //修改指针
            //注意顺序
            prev = cur;
            cur = nnext;
            //注意链表的个数和边界
            if(cur!=null) next = cur.next;
            if(next!=null) nnext = next.next;
        }
        return newhead.next;
    }
}
